package cn.skyclass.module
{
	import cn.skyclass.beans.CourseBean;
	import cn.skyclass.beans.FlexBean;
	import cn.skyclass.beans.UserBean;
	import cn.skyclass.net.URLUtilEx;
	import cn.skyclass.net.UrlParams;
	import cn.skyclass.net.UserToken;
	
	/**
	 * 参数接收器，可以通过 url 或是 模块环境来接收
	 */
	[Bindable]
	public class ParamReceiver extends FlexBean
	{
		// course info
		public var courseId:int = 0;
		public var courseName:String = "";
		public var courseNumber:String = "";
		
		// user info
		public var userId:int = 0;
		public var userNumber:String = "";
		public var userName:String = "";
		public var userTrueName:String = "";
		public var userPassword:String = "";
		public var userType:int = 0;
		public var userNickname:String = "";	
		
		// path info
		public var rootUrl:String = "";
		public var mainAppContextPath:String = "";
		public var moduleContextPath:String = "";
		
		// other info
		public var isUrlParams:Boolean = false;
		protected var cachedParam:ModuleParamInfo = new ModuleParamInfo();
		
		
		/**
		 * 从 url 参数中获取初始化信息
		 */			
		public function getParamFromUrl():void{
			this.isUrlParams = true;
			
			this.rootUrl = URLUtilEx.getRootUrl();
			this.moduleContextPath = URLUtilEx.getContextPath();
			
			var params:UrlParams = new UrlParams(URLUtilEx.getQueryParams());
			this.courseId = params.getIntValue("courseId");
			this.courseNumber = params.getStrValue("courseNumber");
			this.userId = params.getIntValue("userId");
			this.userType = params.getIntValue("userType");
			this.userNumber = params.getStrValue("userNumber");
		}		
		
		/**
		 * 从模块参数中获取初始化信息
		 */
		public function getParamFromModule(param:Object):void{
			this.isUrlParams = false;
			
			if(param){
				this.cachedParam = new ModuleParamInfo();
				this.cachedParam.copyProperties(param);
			}
			
			// copy all module's params to this fields from cached param
			this.copyProperties(this.cachedParam);
			
			// validate path
			this.moduleContextPath = "/" + this.moduleContextPath.replace(/[\\\/]/g, "");
			this.mainAppContextPath = "/" + this.mainAppContextPath.replace(/[\\\/]/g, "");
		}
		
		
		/**
		 * 使用参数生成用户信息
		 */
		public function generateUserBean():UserBean{
			var u:UserBean = new UserBean();
			u.id = this.userId;
			u.name = this.userName;
			u.password = this.userPassword;
			u.trueName = this.userTrueName;
			u.type = this.userType;			
			u.nickname = this.userNickname;			
			u.number = this.userNumber;			
			return u;		
		}
		
		public function generateCourseBean():CourseBean{
			var c:CourseBean = new CourseBean();
			c.id = this.courseId;
			c.name = this.courseName;	
			c.number = this.courseNumber;
			return c;		
		}
		
		public function generateToken():UserToken{
			var t:UserToken = new UserToken();
			t.userId = this.userId;
			t.userName = this.userName;
			t.userNumber = this.userNumber;
			t.encryptedPassword = this.userPassword;
			t.generateToken();
			return t;	
		}		
	}
}